package cn.initcap.elasticjob.job;

import cn.initcap.elasticjob.autoconfig.ElasticDataflowJob;
import cn.initcap.elasticjob.dao.JdOrderMapper;
import cn.initcap.elasticjob.dao.TmallOrderMapper;
import cn.initcap.elasticjob.model.AllOrder;
import cn.initcap.elasticjob.model.JdOrder;
import cn.initcap.elasticjob.model.TmallOrder;
import cn.initcap.elasticjob.service.OrderService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import com.google.common.collect.Lists;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import static java.util.stream.Collectors.toList;

/**
 * @author initcap
 * @date 6/26/19 3:27 PM
 */
@ElasticDataflowJob(
        jobName = "fetchThirdOrderJob",
        cron = "0/15 * * * * ?",
        shardingTotalCount = 2,
        overwrite = true,
        streamingProcess = true
)
public class FetchThirdOrderJob implements DataflowJob<Object> {
    private static final String SYSTEM_OPERATOR = "system";
    @Autowired
    private OrderService orderService;
    @Autowired
    private JdOrderMapper jdOrderMapper;
    @Autowired
    private TmallOrderMapper tmallOrderMapper;

    @Override
    public List<Object> fetchData(ShardingContext shardingContext) {
        //京东订单
        if (shardingContext.getShardingItem() == 0) {
            List<JdOrder> jdOrders = jdOrderMapper.getNotFetchedOrder(5);
            if (jdOrders != null && !jdOrders.isEmpty()) {
                return jdOrders.stream().map(jdOrder -> (Object) jdOrder).collect(toList());
            }
        } else {//天猫订单
            List<TmallOrder> tmallOrders = tmallOrderMapper.getNotFetchedOrder(5);
            if (tmallOrders != null && !tmallOrders.isEmpty()) {
                return tmallOrders.stream().map(tmallOrder -> (Object) tmallOrder).collect(toList());
            }
        }
        return Lists.newArrayList();
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Object> data) {
        //京东订单
        if (shardingContext.getShardingItem() == 0) {
            if (data != null && !data.isEmpty()) {
                List<JdOrder> jdOrders = data.stream().map(d -> (JdOrder) d).collect(toList());
                for (JdOrder jdOrder : jdOrders) {
                    AllOrder allOrder = new AllOrder();
                    allOrder.setThirdOrderId(jdOrder.getId());
                    //京东订单
                    allOrder.setType(0);
                    allOrder.setTotalAmount(jdOrder.getAmount());
                    allOrder.setCreateUser(SYSTEM_OPERATOR);
                    allOrder.setCreateTime(new Date());
                    allOrder.setUpdateUser(SYSTEM_OPERATOR);
                    allOrder.setUpdateTime(new Date());
                    orderService.processJdOrder(allOrder);
                }
            }
        } else {//天猫订单
            if (data != null && !data.isEmpty()) {
                List<TmallOrder> tmallOrders = data.stream().map(d -> (TmallOrder) d).collect(toList());
                for (TmallOrder tmallOrder : tmallOrders) {
                    AllOrder allOrder = new AllOrder();
                    allOrder.setThirdOrderId(tmallOrder.getId());
                    //天猫订单
                    allOrder.setType(1);
                    allOrder.setTotalAmount(tmallOrder.getMoney());
                    allOrder.setCreateUser(SYSTEM_OPERATOR);
                    allOrder.setCreateTime(new Date());
                    allOrder.setUpdateUser(SYSTEM_OPERATOR);
                    allOrder.setUpdateTime(new Date());
                    orderService.processTmallOrder(allOrder);
                }
            }
        }


    }
}
